#include "my_basic_timer.h"
#include "my_gpio.h"

static TIM_TimeBaseInitType TIM_TimeBaseStructure;
static OCInitType TIM_OCInitStructure;
static uint16_t PrescalerValue = 0;

/**
 * @brief  Configures tim3 clocks.
 * 
 * 
 * TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1)/(Prescaler + 1)
 *  duty cycle is equal to: TIM1_CCR1/(TIM1_ARR + 1) = 50%
 */
static void hf_timer_cfg(void)
{
    /* Compute the prescaler value */
    PrescalerValue = 0;//(SystemCoreClock / 640000) - 1; //(uint16_t) (SystemCoreClock / 12000000) - 1;

    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);    
	TIM_TimeBaseStructure.Period    = 255*2;      //TIM1 counter clock / (TIM1_Period + 1) = 250 KHz

    TIM_TimeBaseStructure.Prescaler = 0;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);


   /* Channel 1 Configuration in PWM mode */
    TIM_OCInitStructure.OcMode       = TIM_OCMODE_PWM1;          //使用pwm2模式
    TIM_OCInitStructure.OutputState  = TIM_OUTPUT_STATE_ENABLE;  //pwm 输出使能--内部通过这个直接控制了pwm输出
    TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;    //互补输出使能
    TIM_OCInitStructure.Pulse        = TIM_TimeBaseStructure.Period/2;
    TIM_OCInitStructure.OcPolarity   = TIM_OC_POLARITY_LOW;
    TIM_OCInitStructure.OcNPolarity  = TIM_OCN_POLARITY_LOW;
    TIM_OCInitStructure.OcIdleState  = TIM_OC_IDLE_STATE_SET;
    TIM_OCInitStructure.OcNIdleState = TIM_OC_IDLE_STATE_RESET;

    TIM_InitOc1(TIM1, &TIM_OCInitStructure);
    TIM_ConfigOc1Preload(TIM1, TIM_OC_PRE_LOAD_ENABLE);

    // /* TIM1 enable counter */
    // TIM_Enable(TIM1, ENABLE);

 /* Main Output Enable */
    TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);

}
/**
 * @brief  Configures the different system clocks.
 */
static void hf_timer_rcc_cfg(void)
{
    /* PCLK1 = HCLK/4 */
    RCC_ConfigPclk2(RCC_HCLK_DIV1);	//RCC_HCLK_DIV1

    /* TIM3 clock enable[enable timer1, gpio_a, gpio_b,...] */
    RCC_EnableAPB2PeriphClk(
        RCC_APB2_PERIPH_TIM1|RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
}


/**
 * @brief  Configure the nested vectored interrupt controller.
 */
static void hf_timer_nvic_cfg(void)
{

}


/***********
PB13------HF_PWM_B //---[TIM1_CH1N] ----复用功能 AF2
PA8-------HF_PWM_A //----[TIM1_CH1] -----复用功能 AF2
 * 
 * 
*/
static void hf_gpio_init(void)
{
   

    GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin        = GPIO_PIN_8;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM1;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = GPIO_PIN_13;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM1;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);

}

void my_hf_wave_stop(void)
{
    TIM_Enable(TIM1, DISABLE);

}
void my_hf_wave_start(void)
{
    TIM_Enable(TIM1, ENABLE);
}

void my_hf_init(void)
{
    //必须先把 PA8口设置成输出，然后高周波才能出来
	gpio_output_cfg(GPIOA, GPIO_PIN_8);
    hf_gpio_init();
    hf_timer_rcc_cfg();
    hf_timer_nvic_cfg();
    hf_timer_cfg();
}

